package com.cgnpc.scp.run.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.run.mybatis.entity.MarketReview;
import com.cgnpc.scp.run.mybatis.entity.MarketReviewPeople;
import com.cgnpc.scp.run.mybatis.mapper.client.MarketInfoMapper;
import com.cgnpc.scp.run.mybatis.mapper.client.MarketReviewMapper;
import com.cgnpc.scp.run.mybatis.mapper.client.MarketReviewPeopleMapper;
import com.cgnpc.scp.run.service.MarketReviewService;
import com.cgnpc.scp.run.utils.CommonRunUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**********************************
 * 用途说明: 市场机会评审
 * 作者姓名: zhangyibing
 * 创建时间: 2020年9月13日15:51:35
 ***********************************/
@Service("marketReviewService")
public class MarketReviewServiceImpl implements MarketReviewService {

    @Autowired
    private MarketReviewMapper marketReviewMapper;

    @Autowired
    private MarketReviewPeopleMapper marketReviewPeopleMapper;

    @Autowired
    private MarketInfoMapper marketInfoMapper;

    @Autowired
    private CurrentUser currentUser;

    @Override
    public MarketReview getReviewByInfoId(String infoId) {
        MarketReview review = null;

        List<MarketReview> marketInfos = marketReviewMapper.selectList(new QueryWrapper<MarketReview>()
                .select("*", "(select file_center_file_name from tbl_common_fileupload tcf where tcf.file_center_file_id = tbl_run_market_review.review_file_id) as review_file_name")
        .eq(StringUtil.isNullOrSpace(infoId), "marketinfo_id", infoId));

        if(marketInfos != null && marketInfos.size() != 0){
            review = marketInfos.get(0);
        }else{
            return null;
        }

        List<MarketReviewPeople> peoples = marketReviewPeopleMapper.selectList(new QueryWrapper<MarketReviewPeople>()
        .eq("market_review_id",review.getId()));

        review.setReviewPeopleList(peoples);
        return review;
    }

    @Override
    public AjaxResult saveOrUpdateReview(MarketReview review){
        String infoId = review.getMarketinfoId();
        if(StringUtil.isNullOrSpace(infoId)){
            return AjaxResult.error("marketInfo id can not be null");
        }
        String reviewId = null;

        List<MarketReview> marketInfos = marketReviewMapper.selectList(new QueryWrapper<MarketReview>()
                .select("*", "(select file_center_file_name from tbl_common_fileupload tcf where tcf.file_center_file_id = tbl_run_market_review.review_file_id) as review_file_name")
                .eq(StringUtil.isNullOrSpace(infoId), "marketinfo_id", infoId));
        if(marketInfos != null && marketInfos.size() != 0){
            reviewId = marketInfos.get(0).getId();
        }

        if(StringUtil.isNullOrSpace(reviewId)){
            marketReviewMapper.insert((MarketReview) CommonRunUtil.saveOrUpdateData(review, currentUser));
            List<MarketReviewPeople> peoples = review.getReviewPeopleList();
            if(peoples != null && peoples.size() != 0){
                savePeoples(peoples, review.getId());
            }
        }else{
            review.setId(reviewId);
            marketReviewMapper.updateById(
                    (MarketReview) CommonRunUtil.saveOrUpdateData(review, currentUser)
            );
            List<MarketReviewPeople> peoples = review.getReviewPeopleList();
            if(peoples != null && peoples.size() != 0){
                marketReviewPeopleMapper.delete(new QueryWrapper<MarketReviewPeople>().eq("market_review_id", review.getId()));

                savePeoples(peoples, review.getId());
            }
        }
        return AjaxResult.success();
    }

    private void savePeoples(List<MarketReviewPeople> peoples, String reviewId){
        for(MarketReviewPeople people : peoples){
            people.setMarketReviewId(reviewId);
            people.setId(IDHelper.uuid());
            marketReviewPeopleMapper.insert(people);
        }
    }
}
